<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{ active: isLogin }">
        <div class="big-contain" key="bigContainLogin" v-if="isLogin">
          <div class="btitle">在线考试系统登录</div>
          <div class="bform">
            <input type="text" placeholder="用户姓名" v-model="form.username" />
            <span class="errTips" v-if="nameError">* 用户姓名填写错误 *</span>
            <input type="password" placeholder="密码" v-model="form.userpwd" />
            <span class="errTips" v-if="nameError">* 密码填写错误 *</span>
          </div>
          <button class="bbutton" @click="login">登录</button>
        </div>
        <div class="big-contain" key="bigContainRegister" v-else>
          <div class="btitle">注册账号</div>
          <div class="bform">
            <input type="text" placeholder="用户姓名" v-model="form.username" />
            <span class="errTips" v-if="existed">* 用户姓名已经存在！ *</span>
            <input type="password" placeholder="密码" v-model="form.userpwd" />
            <input type="password" placeholder="确认密码" v-model="form.repeat" />
            <el-radio-group v-model="radio" @change="display" class="shenfen">
              <el-radio :label="3">老师</el-radio>
              <el-radio :label="6">学生</el-radio>
            </el-radio-group>
            <el-select
              v-show="isShow"
              v-model="value"
              placeholder="请选择班级"
              class="banji"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
          <button class="bbutton" @click="register">注册</button>
        </div>
      </div>
      <div class="small-box" :class="{ active: isLogin }">
        <div class="small-contain" key="smallContainRegister" v-if="isLogin">
          <div class="stitle">你好!</div>
          <p class="scontent">温馨提示：如果您还没有登录账号，请点击下面的注册按钮进行注册</p>
          <button class="sbutton" @click="changeType">注册</button>
        </div>
        <div class="small-contain" key="smallContainLogin" v-else>
          <div class="stitle">你好!</div>
          <p class="scontent">温馨提示：如果您已拥有登录账号，请点击下面的注册按钮进行注册</p>
          <button class="sbutton" @click="changeType">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "login-register",
  data() {
    return {
      isLogin: true,
      nameError: false,
      passwordError: false,
      existed: false,
      form: {
        username: "",
        userpwd: "",
        repeat: "",
      },
      isShow: false,
      radio: 3,
      options: [
        { value: "选项1", label: "一年级" },
        { value: "选项2", label: "二年级" },
        { value: "选项3", label: "三年级" },
      ],
      value: "",
    };
  },
  methods: {
    changeType() {
      this.isLogin = !this.isLogin;
      this.form.username = "";
      this.form.userpwd = "";
      this.form.repeat = "";
    },
    login() {
      const self = this;
      if (self.form.username != "" && self.form.userpwd != "") {
        self
          .$axios({
            method: "post",
            url: "http://127.0.0.1:10520/api/user/login",
            data: {
              username: self.form.username,
              password: self.form.userpwd,
            },
          })
          .then((res) => {
            switch (res.data) {
              case 0:
                alert("登陆成功！");
                break;
              case -1:
                this.nameError = true;
                break;
              case 1:
                this.passwordError = true;
                break;
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        alert("填写不能为空！");
      }
    },
    display: function (val) {
      this.isShow = val === 6 ? true : false;
    },
    register() {
      if (
        this.form.username != "" &&
        this.form.repeat != "" &&
        this.form.userpwd != ""
      ){
        if(this.form.userpwd ===this.form.repeat){
          this.form.username=""
          this.form.userpwd=""
          alert('注册成功！')
          this.isLogin=true;
                }else{alert("两次密码不一致");}
        }else{alert("填写不能为空！")}
      }
    }
}
</script>

<style scoped>
*{
  font-family: '隶书';
}
.login-register {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
}
.contain {
  width: 60%;
  height: 60%;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0, 0 0 6px #f0f0f0;
}
.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}
.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.btitle {
  font-size: 1.5em;
  font-weight: bold;
  color: rgb(57, 167, 176);
  margin-bottom: 12px;
  margin-top: 0px;
}
.bform {
  width: 100%;
  height: 60%;
  padding: 1px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.bform .errTips {
  display: block;
  width: 50%;
  text-align: left;
  color: red;
  font-size: 0.7em;
  margin-left: 1em;
}
.bform input {
  width: 50%;
  height: 30px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}
.shenfen {
  width: 40%;
  height: 22px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
  margin-bottom: 35px;
  margin-left: -80px;
  padding-top: 10px;
  padding-left: 2px;
}
.banji {
  position: absolute;
  width: 40%;
  height: 25px;
  margin-top: 195px;
  margin-left: -80px;
  color: black;
}
.bbutton {
  width: 20%;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: #409EFF;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
  margin-top: 25px;
}
.small-box {
  width: 30%;
  height: 100%;
  font-size: 16px;
  background: linear-gradient(135deg, #409EFF, #409EFF);
  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}
.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}
.scontent {
  font-size: 0.8em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}
.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.big-box.active {
  left: 0;
  transition: all 0.5s;
}
.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
</style>
